#include "remap.h"

void init() {
    proto::User user;
    proto::User1 user1;
    user1.set_field1(111);
    user1.set_field2(222);
    user1.set_field3(333);
    user.add_users()->PackFrom(user1);
    proto::User2 user2;
    user2.set_field1(444);
    user2.set_field2(555);
    user2.set_field3(666);
    user.add_users()->PackFrom(user2);
    std::ofstream vFile("users.bin", std::ios::binary);
    user.SerializeToOstream(&vFile);
    vFile.close();
}

void convert(const std::string &path) {
    proto::User user;
    std::ifstream vFile("users.bin", std::ios::binary);
    user.ParseFromIstream(&vFile);
    rosbag::Bag bag;
    bag.open(path, rosbag::bagmode::Write);
    std::map<std::string, RemapBase *> userList{
        {"user1", new UserImpl1()},
        {"user2", new UserImpl2()},
    };
    for (auto msg : user.users()) {
        if (msg.Is<proto::User1>()) {
            proto::User1 user1;
            msg.UnpackTo(&user1);
            userList["user1"]->remap(bag, user1);
        }
        if (msg.Is<proto::User2>()) {
            proto::User2 user2;
            msg.UnpackTo(&user2);
            userList["user2"]->remap(bag, user2);
        }
    }
    bag.close();
}

int main(int argc, char *argv[]) {
    ros::init(argc, argv, "demo");
    ros::NodeHandle nh;
    std::string path = "users.bag";
    init();
    convert(path);
    
    return 0;
}
